
import { find, _decorator } from 'cc';
import { IBehaviorTree } from '../../../../app-core/behaviorA/base/IBehaviorTree';
import { BT_State } from '../../../../app-core/behaviorA/BTDefine';
import { BTFactory } from '../../../../app-core/behaviorA/BTFactory';
import { Selector } from '../../../../app-core/behaviorA/impl/composite/Selector';
import { Decorator } from '../../../../app-core/behaviorA/impl/decorator/Decorator';
import { Task } from '../../../../app-core/behaviorA/impl/task/Task';
import { Logger } from '../../../../app-core/common/log/Logger';
import { injectPresenter } from '../../../../app-core/inject';
import { BaseView } from '../../../../app-core/mvp/base/BaseView';
import * as LoginContract from '../contract/LoginContract';
import LoginPresenter from '../presenter/LoginPresenter';
const { ccclass } = _decorator;

// import { StateMachine } from "../../../../app-core/common/fsm/state-machine";


/**
 * Predefined variables
 * Name = LoginViewer
 * DateTime = Mon Nov 15 2021 16:36:04 GMT+0800 (中国标准时间)
 * Author = baimingjiang
 * FileBasename = LoginViewer.ts
 * FileBasenameNoExtension = LoginViewer
 * URL = db://assets/app/ui/viewer/LoginViewer.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('LoginViewer')
export class LoginViewer extends BaseView implements LoginContract.ILoginView {

    @injectPresenter(LoginPresenter)
    protected loginPresenter: LoginContract.ILoginPresenter;

    onLoad() {
        super.onLoad();

        var btnNode = find('Button', this.node);
        // var btnComp: Button = btnNode.getComponent(Button);
        // btnNode.on(Node.EventType.TOUCH_END, (event) => {
        //     // this.ui.toast("hahahaha ");
        //     this.removeSelf(true);
        // });

        btnNode.on('click', this.clickCallback, this);


        this.creatBehavior();

    }

    clickCallback(event) {

        this.loginPresenter.toLogin();

        // let door = new Door();

        // console.log('door.isOpen:' + door.isOpen());
        // console.log('door.isBroken:' + door.isBroken());
        // console.log('door.can(Events.open):' + door.can(Events.open));

        // console.log('door open...')
        // door.open().then(() => {
        //     console.log('door.isOpen' + door.isOpen());
        // });
    }

    loginSuccess() {
        Logger.info('login success');
        // this.removeSelf();
        // app.ui.open(UIID.Battle, { viewer: BattleViewer });

        this.runTree();

    }

    loginFail(msg: string) {
        Logger.error(msg);
    }


    tree: IBehaviorTree = null;
    creatBehavior() {

        let root = new Selector();

        // 初始化所有节点
        let randomTime = new RandomMoveTime;
        let startMove = new StartMoveTask;
        let attack = new AttackTask();

        // 组织关系
        root.addChild(randomTime);
        root.addChild(attack);

        randomTime.setChild(startMove);

        // 创建bt
        this.tree = BTFactory.creatWithRoot(root);

    }

    runTree() {
        if (this.tree) {
            this.tree.start((ret) => {
                Logger.debug('执行结束', ret);
            });
        }
    }

    update(dt) {
        if (this.tree && this.tree.isRunning()) {
            this.tree.tick(dt);
        }
    }

}



class StartMoveTask extends Task {
    isProcess = false;

    lastTime = 0;

    public onEnter(): void {
        Logger.debug('开始巡逻。。。', this.lastTime, '秒');
    }

    public onExit(): void {
        Logger.debug('结束巡逻。。。');
    }

    public onProcess(dt: number): void {
        // Logger.debug('正在巡逻。。。。。', this.lastTime);

        this.lastTime -= dt;
        if (this.lastTime < 0) {
            this.setResult(BT_State.SUCCESS);
        }

    }

}

class AttackTask extends Task {

    lastTime = 10;

    public onEnter(): void {
        Logger.debug('开始攻击。。。', this.lastTime, '秒')
    }

    public onExit(): void {
        Logger.debug('结束攻击。。。')
    }

    public onProcess(dt: number): void {
        this.lastTime -= dt;
        if (this.lastTime < 0) {
            this.setResult(BT_State.SUCCESS);
        }
    }

    public reset(): void {
        super.reset();
        this.lastTime = 10;
    }

}

class RandomMoveTime extends Decorator {

    canRun = false;

    public onEnter(): void {
        Logger.debug('装饰子节点。。。')

        let child = <StartMoveTask>this._child;
        child.lastTime = Math.random() * 10;

        this.canRun = Math.random() * 100 > 50;
    }

    public onExit(): void {
        Logger.debug('退出装饰。。。')
    }

    public onProcess(dt: number): void {
        if (this.canRun) {
            // Logger.debug('执行子节点')
            this._child.setContext(this._context);
            let ret = this._child.tick(dt);
            if (ret === BT_State.SUCCESS || ret === BT_State.FAILURE || ret === BT_State.ABORT || ret === BT_State.ERROR) {
                this.setResult(ret);
            }
        } else {
            this.setResult(BT_State.FAILURE);
            Logger.debug('装饰返回失败')
        }
    }

}